package org.jeecg.modules.carInformation.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.carInformation.entity.CarInformation;
import org.jeecg.modules.carInformation.service.ICarInformationService;
import org.jeecg.modules.carPersonnel.entity.CarPersonnel;
import org.jeecg.modules.carPersonnel.service.ICarPersonnelService;
import org.jeecg.modules.carRegister.util.QueryEnv;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

 /**
 * @Description: 车辆信息采集数据表
 * @Author: jeecg-boot
 * @Date:   2024-07-23
 * @Version: V1.0
 */
@Api(tags="车辆信息采集数据表")
@RestController
@RequestMapping("/carInformation/carInformation")
@Slf4j
public class CarInformationController extends JeecgController<CarInformation, ICarInformationService> {
	@Autowired
	private ICarInformationService carInformationService;
	 @Autowired
	 private ICarPersonnelService carPersonnelService;
	 @Autowired
	 QueryEnv queryEnv;

	/**
	 * 分页列表查询
	 *
	 * @param carInformation
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "车辆信息采集数据表-分页列表查询")
	@ApiOperation(value="车辆信息采集数据表-分页列表查询", notes="车辆信息采集数据表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(CarInformation carInformation,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<CarInformation> queryWrapper = QueryGenerator.initQueryWrapper(carInformation, req.getParameterMap());
		Page<CarInformation> page = new Page<CarInformation>(pageNo, pageSize);
		IPage<CarInformation> pageList = carInformationService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param carInformation
	 * @return
	 */
	@AutoLog(value = "车辆信息采集数据表-添加")
	@ApiOperation(value="车辆信息采集数据表-添加", notes="车辆信息采集数据表-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody CarInformation carInformation) {
		carInformationService.save(carInformation);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param carInformation
	 * @return
	 */
	@AutoLog(value = "车辆信息采集数据表-编辑")
	@ApiOperation(value="车辆信息采集数据表-编辑", notes="车辆信息采集数据表-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody CarInformation carInformation) {
		carInformationService.updateById(carInformation);
		return Result.OK("编辑成功!");
	}

	 @AutoLog(value = "车辆信息采集数据表-app-编辑")
	 @ApiOperation(value="车辆信息采集数据表-app-编辑", notes="车辆信息采集数据表-app-编辑")
	 @PutMapping(value = "/m/edit")
	 public Result<?> mEdit(@RequestBody CarInformation carInformation) {
		 carInformationService.updateById(carInformation);
		 return Result.OK("编辑成功!");
	 }

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "车辆信息采集数据表-通过id删除")
	@ApiOperation(value="车辆信息采集数据表-通过id删除", notes="车辆信息采集数据表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		carInformationService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "车辆信息采集数据表-批量删除")
	@ApiOperation(value="车辆信息采集数据表-批量删除", notes="车辆信息采集数据表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.carInformationService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "车辆信息采集数据表-通过id查询")
	@ApiOperation(value="车辆信息采集数据表-通过id查询", notes="车辆信息采集数据表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		CarInformation carInformation = carInformationService.getById(id);
		if(carInformation==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(carInformation);
	}

	 @AutoLog(value = "车辆信息采集数据表-对外-通过车牌号查询")
	 @ApiOperation(value="车辆信息采集数据表-对外-通过车牌号查询", notes="车辆信息采集数据表-对外-通过车牌号查询")
	 @GetMapping(value = "/m/queryByPlate")
	 public Result<?> queryByPlate(@RequestParam(name="plate",required=true) String plate) {
		 CarInformation carInformation = carInformationService.getByPlate(plate);
		 if(carInformation==null) {
			 return Result.error("未找到对应数据");
		 }
		 Page<CarInformation> page = new Page<CarInformation>(1, 10);
		 IPage<CarInformation> pageList = carInformationService.page(page, new QueryWrapper<CarInformation>().eq("plate_no", plate));
		 return Result.OK(pageList);
	 }

    /**
    * 导出excel
    *
    * @param request
    * @param carInformation
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, CarInformation carInformation) {
        return super.exportXls(request, carInformation, CarInformation.class, "车辆信息采集数据表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, CarInformation.class);
    }

	@PostMapping("/m/addCar")
	public Result<?> addCar(@RequestBody JSONObject jsonObject) {
		String openId = jsonObject.getString("openId");
		String plateNo = jsonObject.getString("plateNo");
		CarInformation byPlate = carInformationService.getByPlate(plateNo);
		if (byPlate == null) {
			String env = getEnv(jsonObject);
			jsonObject.put("evnType", env);
			CarInformation carInformation = jsonObject.toJavaObject(CarInformation.class);
			carInformationService.save(carInformation);
		}
		CarPersonnel carPersonnel = carPersonnelService.getByOpenIdAndPlateNo(openId, plateNo);
		if (carPersonnel == null) {
			CarPersonnel carPersonnel1 = new CarPersonnel();
			carPersonnel1.setOpenId(openId);
			carPersonnel1.setPlateNo(plateNo);
			carPersonnelService.save(carPersonnel1);
		}
		return Result.OK();
	}

	 private String getEnv(JSONObject jsonObject) {
		 //新能源车辆，直接通过
		 String plateColor = jsonObject.getString("plateColor");
		 if ("4".equals(plateColor) || "6".equals(plateColor)) {
			 return "D";
		 }
		 String plateNo = jsonObject.getString("plateNo");
		 String local = queryEnv.getLocal(plateNo);
		 if (local != null) {
			 return local;
		 }
		 String location = jsonObject.getString("location");
		 String zhongjiao = queryEnv.getZhongJiao(plateNo, plateColor, location);
		 if (zhongjiao != null) {
			 return zhongjiao;
		 }
		 String vin = jsonObject.getString("vin");
		 String angmei = queryEnv.getAngMei(vin, plateNo, plateColor, location);
		 if (angmei != null) {
			 return angmei;
		 }
		 return "X";
	 }

}
